sap.ui.define(
    [
        "sap/ui/core/mvc/Controller",
        "sap/ui/model/json/JSONModel",
        'sap/ui/core/Fragment',
        'sap/m/Input',
        'sap/m/ColumnListItem',
        'sap/m/MessagePopover',
        'sap/m/Link',
        'sap/m/MessageItem',
        "sap/m/MessageToast",
        "sap/ui/core/routing/Router"
    ],
    function (Controller, JSONModel, Fragment, Input, ColumnListItem, MessagePopover, Link, MessageItem, MessageToast,Router) {
        "use strict";
        var oMessagePopover
        var orderId

        return Controller.extend("ikdproject200.controller.Detail", {
            onInit: function () {

                // this.oTable = this.byId("itemTable");
                // debugger
                this.getOwnerComponent().getRouter().getRoute("RouteDetail").attachPatternMatched(this._onRouteMatched, this);



            },
            onAfterRendering: function () {

            },
            onNavBack:function(){
                alert('ddd')
            },
            _onRouteMatched: function (oEvent) {
                var that = this
                this.tableLine = 0
                this.tableLineReverse = 0

                // this.oModelValueHelp = this._loadJSON("ikdproject200/ValueHelp.json")

                var oView = this.getView()

                var Uuid = oEvent.getParameter("arguments").Uuid;
                // 检查是否包含CTNO_  
                if (Uuid.includes("CTNO_")) {
                //     var oGlobalBusyDialog = new sap.m.BusyDialog();
                // oGlobalBusyDialog.open();
                    // 提取aaa22部分
                    let match = Uuid.match(/CTNO_(.*)/);
                    if (match) {
                        let extracted = match[1];
                        Uuid = extracted
                        console.log(extracted);
                        oView.setBusy(true);
                        this.getView().getModel().callFunction("/call_api",   //call_api는 behavior에서 정의한 액션 
                    {
                        method: "POST", // 무조건 POST 사용 
                        urlParameters: {   //정의한 파라미터 , 무조건 첫문자만 대문자로 써야됨 
                            "Cargmtno": Uuid,
                            "Mblno": "A"
                        },
                        success: function (odata, response) {
                            //Model Refresh
                            that.getView().getModel().refresh(true);
                            // oGlobalBusyDialog.close();
                            that.getView().getModel().read("/Main", {
                                filters: [ 
                                    new sap.ui.model.Filter({
                                        path: "Cargmtno",
                                        operator: sap.ui.model.FilterOperator.EQ,
                                        value1: Uuid
                                    })
                                ],
                                urlParameters: {
                                    "$select": "Uuid" 
                                },
                                success: function(oData) {
                                    console.log(oData)
                                    Uuid = oData.results[0].Uuid
                                    var sPath = "/Main(guid'" + Uuid + "')"
                                    oView.bindElement({
                                        path: sPath,
                                        events: {
                                            change: function (oEvent) {
                                                oView.setBusy(false);
                                                // that.getView().setModel(that.sData, "config");
                                                // var aData = oEvent.getSource().getModel().oData[sObject]
                                                // // debugger
                    
                                                // var dayDiff = that._calculateDaysFromToday(new Date(aData.Vdatu))
                                                // console.log(dayDiff)
                                                // that.byId("deliveryTime").setText(dayDiff + " Days")
                    
                                            },
                                            dataRequested: function (oEvent) {
                    
                                                // oView.setBusy(true);

                                            },
                                            dataReceived: function (oEvent) {
                    
                                                oView.setBusy(false);
                                                // oGlobalBusyDialog.close();
                                                // that.getView().getModel().read("/I_Customer")
                    
                                            }
                                        }
                                    });
                    
                                    
                                } 
                            });
                        },
                        error: function (res) {
                            console.log(res)
                        }
                    })

                    }
                }else{

                    console.log("detailthis._orderId:======", Uuid)

                    var sPath = "/Main(guid'" + Uuid + "')"
    
                    oView.bindElement({
                        path: sPath,
                        events: {
                            change: function (oEvent) {
    
                                // that.getView().setModel(that.sData, "config");
                                // var aData = oEvent.getSource().getModel().oData[sObject]
                                // // debugger
    
                                // var dayDiff = that._calculateDaysFromToday(new Date(aData.Vdatu))
                                // console.log(dayDiff)
                                // that.byId("deliveryTime").setText(dayDiff + " Days")
    
                            },
                            dataRequested: function (oEvent) {
    
                                oView.setBusy(true);
                            },
                            dataReceived: function (oEvent) {
    
                                oView.setBusy(false);
                                // that.getView().getModel().read("/I_Customer")
    
                            }
                        }
                    });
    
                }




                // this.headerInfo = this.getView().byId("headerInfo")
                // this.headerActionEdit = this.getView().byId("headerActionEdit")
                // this.statusBox = this.getView().byId("statusBox")
                // this.headerFormEdit = this.getView().byId("headerFormEdit")
                // this.detailDisplayBox = this.getView().byId("detailDisplayBox")
                // this.detailInputBox = this.getView().byId("detailInputBox")
                // this.additionalDisplayBox = this.getView().byId("additionalDisplayBox")
                // this.additionalInputBox = this.getView().byId("additionalInputBox")

                // // this.itemTableEdit = this.getView().byId("itemTableEdit");
                // this.oTable = this.byId("itemTable");

            },
            onClickCont: function () {
                var that = this

                var oGlobalBusyDialog = new sap.m.BusyDialog();
                oGlobalBusyDialog.open();

                // that.getView().setBusy(true)
                this.pDialog = this.loadFragment({
                    name: "ikdproject310.view.Fragments.DialogCon"
                });
                this.pDialog.then(function (oDialog) {
                    that.oDialog = oDialog

                    that.getView().getModel("Cont").callFunction("/call_api",   //call_api는 behavior에서 정의한 액션 
                        {
                            method: "POST", // 무조건 POST 사용 
                            urlParameters: {   //정의한 파라미터 , 무조건 첫문자만 대문자로 써야됨 
                                "Cargmtno": that.getView().byId('Cargmtno').getText()
                            },
                            success: function (odata, response) {
                                //Model Refresh
                                that.getView().getModel("Cont").refresh(true);
                                oGlobalBusyDialog.close();
                                // that.getView().setBusy(false)
                                oDialog.open();
                            },
                            error: function (res) {
                                console.log(res)
                            }
                        })


                })



            },
            onDialogClose: function () {

                this.oDialog.close();

            },
            onAfterClose: function () {
                this.tableLine = 0
                this.oDialog.destroy();
            },
            formatFloat: function (value) {

                return new sap.ui.model.type.Float({
                    groupingEnabled: true
                }).formatValue([value], "string");

            },
            formatDate: function (value) {
                if (value != null) {
                    return value.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1-$2-$3");
                }
            },
            formatDateTime: function (value) {

                if (value != null) {
                    return value.replace(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/, "$1-$2-$3 $4:$5:$6");
                }

            },
            formatRowIndexReverse: function (value) {
                var oTable = this.getView().byId('itemTable')
                var lines = oTable.getBinding("items").getCount()
                console.log(oTable)
                if (this.tableLineReverse == 0) {
                    this.tableLineReverse = lines
                } else {
                    this.tableLineReverse -= 1
                }

                return this.tableLineReverse

            },
            formatRowIndex: function (value) {

                this.tableLine += 1


                return this.tableLine

            }

        });
    }
);
